/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.composites;

import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.jface.text.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.UseCase;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.commands.MoveInListCommand;
import com.cea.papyrus.core.editor.CommandStackUtils;
import com.cea.papyrus.core.ui.dialogs.ChooseSetUseCasesDialog;
import com.cea.papyrus.umlutils.Element;

/**
 * Displays the use cases of a classifier in a composite.
 * 
 * @author Gilles Cannenterre
 * @since 21 nov. 06
 */
public class UseCasesComposite extends DecoratedTableComposite implements ISectionComposite {

	/**
	 * The Constructor.
	 */
	public UseCasesComposite() {
		super("Use Cases");
	}

	/**
	 * The Constant KEY.
	 */
	private final static String KEY = "usecase_data";
	
	/**
	 * The uml classifier.
	 */
	private Classifier umlClassifier;

	/**
	 * Sets the element.
	 * 
	 * @param element the element
	 */
	@Override
	public void setElement(Element element) {
		super.setElement(element);
		this.umlClassifier = (Classifier)element.getUml2Element();
	}

	/**
	 * Refresh.
	 */
	@Override
	public void refresh() {
		if( !table.isDisposed()&& (umlClassifier!=null)){
			// Remove use cases from table 
			table.removeAll();

			// Read and add use cases
			Iterator<UseCase> it = umlClassifier.getUseCases().iterator();
			while (it.hasNext()) {
				UseCase useCase = it.next();
				TableItem item = new TableItem(table, SWT.BORDER);
				item.setImage(PapyrusPlugin.getImage(PapyrusPlugin.UML_ICONS_16x16+"UseCase.gif"));
				item.setData(KEY, useCase);
				item.setText(useCase.getName());
			}
		}
	}
	
	/**
	 * Adds the button pressed.
	 */
	@Override
	public void addButtonPressed() {
		ChooseSetUseCasesDialog dialog = new ChooseSetUseCasesDialog(composite.getShell(), getElement().getUml2Element());
		dialog.create();
		dialog.setFocus();
		dialog.open();
		if(dialog.hasElementChanged()) {
			refresh();
		}
	}

	/**
	 * Down button pressed.
	 */
	@Override
	public void downButtonPressed() {
		CompoundCommand compoundCommand = new CompoundCommand();
		TableItem[] tableItems = table.getSelection();

		for(int i = (tableItems.length-1); i >= 0; i--) {
			// Get use case list
			List useCases = umlClassifier.getUseCases();
			// Get use case
			UseCase useCase = (UseCase) tableItems[i].getData(KEY);
			int newIndex = table.indexOf(tableItems[i]) + 1;
			if(newIndex < useCases.size()) {
				// Move
				compoundCommand.add(new MoveInListCommand(useCases, useCase, newIndex));
			}
		}
		CommandStack stack = CommandStackUtils.getCommandStack();
		Assert.isNotNull(stack, "Impossible to adapt current editor into a CommandStack");
		stack.execute(compoundCommand);
	}

	/**
	 * Edits the item.
	 * 
	 * @param item the item
	 */
	@Override
	public void editItem(TableItem item) {
		if (item == null) {
			return;
		}
		// Get use case linked to the item
		UseCase useCase = (UseCase)item.getData(KEY);
		// TODO edit a use case
	}

	/**
	 * Removes the button pressed.
	 */
	@Override
	public void removeButtonPressed() {
		// Get selection and remove it
		TableItem[] tableItems = table.getSelection();
		for(int i = 0; i < tableItems.length ; i++) {
			// Get use case remove umlClassifier from its subject list 
			// And remove it from the use case list
			UseCase useCase = (UseCase) tableItems[i].getData(KEY);
			umlClassifier.getUseCases().remove(useCase);
			useCase.getSubjects().remove(umlClassifier);		
		}
	}

	/**
	 * Up button pressed.
	 */
	@Override
	public void upButtonPressed() {
		CompoundCommand compoundCommand = new CompoundCommand();
		TableItem[] tableItems = table.getSelection();
		// Get use case list
		List useCases = umlClassifier.getUseCases();
		// Get use case
		for(int i = 0; i < tableItems.length ; i++) {
			// get use case
			UseCase useCase = (UseCase) tableItems[i].getData(KEY);
			int newIndex = table.indexOf(tableItems[i]) - 1;
			if (newIndex >= 0) {
				compoundCommand.add(new MoveInListCommand(useCases, useCase, newIndex));
			}
		}
		CommandStack stack = CommandStackUtils.getCommandStack();
		Assert.isNotNull(stack, "Impossible to adapt current editor into a CommandStack");
		stack.execute(compoundCommand);
	}
}